import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

public class StackPractice {
    //判断是否是合法的出栈序列
    public static boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        //定义一个栈
        Stack<Integer> stack = new Stack<>();
        //遍历入栈序列的数组
        int i = 0,j = 0;
        for(;i < pushV.length;i++) {
            //i所指向的元素入栈
            stack.push(pushV[i]);
            //判断栈顶元素和j所指的出栈序列数组中的元素是否相等
            while(!stack.isEmpty() && stack.peek() == popV[j] && j < popV.length) {
                //栈顶元素出栈
                stack.pop();
                //j后移
                j++;
            }
        }

        //最后如果栈为空的话，则是正确的出栈序列，若栈不为空则不是正确的出栈序列
        return stack.isEmpty();
        //
    }

    public static boolean isNumber(String s) {
        boolean result = true;
        //利用Integer判断是否为整数
        try {
            Integer.parseInt(s);
        }catch (NumberFormatException e) {
            result = false;
        }

        return result;
    }
    public static int evalRPN1(String[] tokens) {
        //定义一个栈
        Stack<Integer> stack = new Stack<>();
        //遍历数组
        for(String s : tokens) {
            //遇到数字则加入栈
            if(isNumber(s)) {
                stack.push(Integer.parseInt(s));
            }else {
                //遇到运算符则弹出栈顶的两个元素 a,b 假设遇到的运算符为op,将b op a压入栈
                String op = s;
                int a = stack.pop();
                int b = stack.pop();
                switch (op) {
                    case "+" :
                        stack.push(b + a);
                        break;
                    case  "-" :
                        stack.push(b - a);
                        break;
                    case "*" :
                        stack.push(b * a);
                        break;
                    case "/":
                        stack.push(b / a);
                        break;
                }
            }
        }

        //在遍历完数组之后，栈中的最后一个元素即为逆波兰表达式的值
        return stack.pop();

    }

    public static int evalRPN(String[] tokens) {
        Deque<Integer> stack = new LinkedList<Integer>();
        int n = tokens.length;
        for (int i = 0; i < n; i++) {
            String token = tokens[i];
            if (isNumber1(token)) {
                stack.push(Integer.parseInt(token));
            } else {
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch (token) {
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                    default:
                }
            }
        }
        return stack.pop();
    }

    public static boolean isNumber1(String token) {
        return !("+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token));
    }


    public static void main2(String[] args) {
        String[] tokens = new String[]{"10","6","9","3","+","-11","*","/","*","17","+","5","+"};
        System.out.println(evalRPN(tokens));
    }

    public static void main1(String[] args) {
        int[] pushV = new int[]{1,2,3,4,5};
        int[] popV = new int[]{4,3,5,1,2};

        boolean result  = IsPopOrder(pushV,popV);
        System.out.println(result);
    }

    //括号匹配问题
    public  static boolean isValid(String s) {
        //奇数个括号不可能匹配
        if(s.length()%2 != 0) {
            return false;
        }

        //定义一个栈
        Stack<Character> stack = new Stack<>();
        //遍历字符串
        for(int i = 0;i < s.length();i++) {
            //遇到左括号则入栈
            char ch = s.charAt(i);
            if(ch == '{' || ch == '(' || ch == '[') {
                stack.push(ch);
            }else {
                //遇到右括号，则与栈顶元素比较，看是否匹配
                //遇到右括号时，栈中没有左括号，则不匹配，算法结束
                if(stack.isEmpty()) {
                    return false;
                }
                char peek = stack.peek();
                if ((ch == '}' && peek == '{') || (ch == ']' && peek == '[') || (ch == ')' && peek == '(') ) {
                    //弹栈
                    stack.pop();
                }else {
                    //左右括号不匹配，算法结束
                    return false;
                }
            }

        }

        //最后栈为空则匹配，栈不为空则不匹配
          return stack.isEmpty();
    }

    public static void main(String[] args) {
        String s = "([)]";
        System.out.println(isValid(s));
    }
}
